package com.cardgame;

import java.util.Collections;
import java.util.Hashtable;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.cardgame.GameView.GameThread;
import com.cardgame.constants.Constants;
import com.player.Player;

@SuppressWarnings("unchecked")
public class Game extends Activity {
	
	//the number of players in the game
	private int playerNumber = 0;

	private Vector playerOrder = new Vector();
	private Vector classDeck;
	
	//list of passive and active skills
	private Vector passiveSkills = new Vector();
	private Vector activeSkills = new Vector();
	
	Hashtable playerActive1;
	Hashtable playerInactive1;
	Hashtable playerActive2;
	Hashtable playerInactive2;
	
	Player oldSelected = null;
	
	int myposition;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		playerNumber = getIntent().getIntExtra("com.cardgame.playerNumber", 0);
		
		//the player number should never be 0
		if (playerNumber == 0)
		{
			finish();
		}
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.game);
		
		Toast toast = Toast.makeText(this, "Game Start", 0);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
		
		initPlayers();
		initSkills();
		initHashtable();
		
		myposition = (int)(playerNumber * Math.random());

		//TODO: for each image set graphics here
		ImageView control = (ImageView)findViewById(R.id.control);
		control.setBackgroundDrawable(getResources().getDrawable(R.drawable.controlpanel));
		
		ImageView animation = (ImageView)findViewById(R.id.animation);
		
		ImageView action1 = (ImageView)findViewById(R.id.action1);
		ImageView action2 = (ImageView)findViewById(R.id.action2);
		ImageView action3 = (ImageView)findViewById(R.id.action3);
		ImageView action4 = (ImageView)findViewById(R.id.action4);
		ImageView action5 = (ImageView)findViewById(R.id.action5);
		ImageView actionme = (ImageView)findViewById(R.id.actionme);
		
		ImageView status1a = (ImageView)findViewById(R.id.status1a);
		ImageView status1b = (ImageView)findViewById(R.id.status1b);
		ImageView status1c = (ImageView)findViewById(R.id.status1c);
		
		ImageView status2a = (ImageView)findViewById(R.id.status2a);
		ImageView status2b = (ImageView)findViewById(R.id.status2b);
		ImageView status2c = (ImageView)findViewById(R.id.status2c);
		
		ImageView status3a = (ImageView)findViewById(R.id.status3a);
		ImageView status3b = (ImageView)findViewById(R.id.status3b);
		ImageView status3c = (ImageView)findViewById(R.id.status3c);
		
		ImageView status4a = (ImageView)findViewById(R.id.status4a);
		ImageView status4b = (ImageView)findViewById(R.id.status4b);
		ImageView status4c = (ImageView)findViewById(R.id.status4c);
		
		ImageView status5a = (ImageView)findViewById(R.id.status5a);
		ImageView status5b = (ImageView)findViewById(R.id.status5b);
		ImageView status5c = (ImageView)findViewById(R.id.status5c);
		
		ImageView statusmea = (ImageView)findViewById(R.id.statusmea);
		ImageView statusmeb = (ImageView)findViewById(R.id.statusmeb);
		ImageView statusmec = (ImageView)findViewById(R.id.statusmec);
		
		ImageView health1 = (ImageView)this.findViewById(R.id.health1);
		ImageView health2 = (ImageView)this.findViewById(R.id.health2);
		ImageView health3 = (ImageView)this.findViewById(R.id.health3);
		ImageView health4 = (ImageView)this.findViewById(R.id.health4);
		ImageView health5 = (ImageView)this.findViewById(R.id.health5);
		ImageView healthme = (ImageView)this.findViewById(R.id.healthme);
		
		ImageView eCard1 = (ImageView)this.findViewById(R.id.cards1);
		ImageView eCard2 = (ImageView)this.findViewById(R.id.cards2);
		ImageView eCard3 = (ImageView)this.findViewById(R.id.cards3);
		ImageView eCard4 = (ImageView)this.findViewById(R.id.cards4);
		ImageView eCard5 = (ImageView)this.findViewById(R.id.cards5);
		ImageView timerme = (ImageView)this.findViewById(R.id.timerme);
		
		ImageButton playerMe = (ImageButton)findViewById(R.id.playerme);
		playerMe.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {  
   
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	
            	boolean selectedValue = ((Player)playerOrder.get(myposition)).getSelected();
            	((Player)playerOrder.get(myposition)).setSelected(!selectedValue);
             	
            	oldSelected = (Player)playerOrder.get(myposition);
            	updatePlayerImage((Player)playerOrder.get(myposition));
            }
        });
		
		Player me = (Player)playerOrder.get(myposition);
		if(me.getTeam() == 1)
			playerMe.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(me.getpClass())));
		else
			playerMe.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(me.getpClass())));
		
		me.setAction(actionme);
		me.setHpImage(healthme);
		me.setPortrait(playerMe);
		me.setStatusa(statusmea);
		me.setStatusb(statusmeb);
		me.setStatusc(statusmec);
		me.setCardNumber(timerme);	
		
		
		ImageButton player5 = (ImageButton)findViewById(R.id.player5);
		player5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	
            	int position = getPosition(1);
            	
            	boolean selectedValue = ((Player)playerOrder.get(position)).getSelected();
            	((Player)playerOrder.get(position)).setSelected(!selectedValue);
            	
            	oldSelected = (Player)playerOrder.get(position);
            	updatePlayerImage((Player)playerOrder.get(position));
            }
        });
		int player5position = getPosition(1);
		
		Player p5 = (Player)playerOrder.get(player5position);
		if(p5.getTeam() == 1)
			player5.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p5.getpClass())));
		else
			player5.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p5.getpClass())));
		p5.setAction(action5);
		p5.setHpImage(health5);
		p5.setPortrait(player5);
		p5.setStatusa(status5a);
		p5.setStatusb(status5b);
		p5.setStatusc(status5c);
		p5.setCardNumber(eCard5);
		

		ImageButton player4 = (ImageButton)findViewById(R.id.player4);
		player4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	int position = getPosition(2);
            	
            	boolean selectedValue = ((Player)playerOrder.get(position)).getSelected();
            	((Player)playerOrder.get(position)).setSelected(!selectedValue);
            	
            	oldSelected = (Player)playerOrder.get(position);
            	updatePlayerImage((Player)playerOrder.get(position));
            }
        });
		
		int player4position = getPosition(2);
		
		Player p4 = (Player)playerOrder.get(player4position);
		if(p4.getTeam() == 1)
			player4.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p4.getpClass())));
		else
			player4.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p4.getpClass())));
		p4.setAction(action4);
		p4.setHpImage(health4);
		p4.setPortrait(player4);
		p4.setStatusa(status4a);
		p4.setStatusb(status4b);
		p4.setStatusc(status4c);
		p4.setCardNumber(eCard4);
		
		ImageButton player3 = (ImageButton)findViewById(R.id.player3);
		player3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	int position = getPosition(3);
            	
            	boolean selectedValue = ((Player)playerOrder.get(position)).getSelected();
            	((Player)playerOrder.get(position)).setSelected(!selectedValue);
            	
            	oldSelected = (Player)playerOrder.get(position);
            	updatePlayerImage((Player)playerOrder.get(position));
            }
        });
		
		int player3position = getPosition(3);
		
		Player p3 = (Player)playerOrder.get(player3position);
		if(p3.getTeam() == 1)
			player3.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p3.getpClass())));
		else
			player3.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p3.getpClass())));
		p3.setAction(action3);
		p3.setHpImage(health3);
		p3.setPortrait(player3);
		p3.setStatusa(status3a);
		p3.setStatusb(status3b);
		p3.setStatusc(status3c);
		p3.setCardNumber(eCard3);
		
		ImageButton player2 = (ImageButton)findViewById(R.id.player2);
		player2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	int position = getPosition(4);
            	
            	boolean selectedValue = ((Player)playerOrder.get(position)).getSelected();
            	((Player)playerOrder.get(position)).setSelected(!selectedValue);
            	
            	oldSelected = (Player)playerOrder.get(position);
            	updatePlayerImage((Player)playerOrder.get(position));
            }
        });
		
		int player2position = getPosition(4);
		
		Player p2 = (Player)playerOrder.get(player2position);
		if(p2.getTeam() == 1)
			player2.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p2.getpClass())));
		else
			player2.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p2.getpClass())));
		p2.setAction(action2);
		p2.setHpImage(health2);
		p2.setPortrait(player2);
		p2.setStatusa(status2a);
		p2.setStatusb(status2b);
		p2.setStatusc(status2c);
		p2.setCardNumber(eCard2);
		
		ImageButton player1 = (ImageButton)findViewById(R.id.player1);
		player1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(oldSelected != null)
            	{
            		oldSelected.setSelected(false);
            		updatePlayerImage(oldSelected);
            	}
            	int position = getPosition(5);
            	
            	boolean selectedValue = ((Player)playerOrder.get(position)).getSelected();
            	((Player)playerOrder.get(position)).setSelected(!selectedValue);
            	
            	oldSelected = (Player)playerOrder.get(position);
            	updatePlayerImage((Player)playerOrder.get(position));
            }
        });
		
		int player1position = getPosition(5);
		
		Player p1 = (Player)playerOrder.get(player1position);
		if(p1.getTeam() == 1)
			player1.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p1.getpClass())));
		else
			player1.setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p1.getpClass())));
		p1.setAction(action1);
		p1.setHpImage(health1);
		p1.setPortrait(player1);
		p1.setStatusa(status1a);
		p1.setStatusb(status1b);
		p1.setStatusc(status1c);
		p1.setCardNumber(eCard1);
		
		//TODO: update the resource for endImage
		ImageButton endImage = (ImageButton)findViewById(R.id.endImage);
		endImage.setVisibility(View.INVISIBLE);
		endImage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	finish();
            }
    	});
		
		ImageButton myCard1 = (ImageButton)this.findViewById(R.id.spell1);
		ImageButton myCard2 = (ImageButton)this.findViewById(R.id.spell2);
		ImageButton myCard3 = (ImageButton)this.findViewById(R.id.spell3);
		ImageButton myCard4 = (ImageButton)this.findViewById(R.id.spell4);
		ImageButton myCard5 = (ImageButton)this.findViewById(R.id.spell5);
		ImageButton myCard6 = (ImageButton)this.findViewById(R.id.spell6);
		ImageButton myCard7 = (ImageButton)this.findViewById(R.id.spell7);
		ImageButton myCard8 = (ImageButton)this.findViewById(R.id.spell8);
		
		ImageButton okButton = (ImageButton)this.findViewById(R.id.confirm1);
		okButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.target));
		
		ImageButton cancelButton = (ImageButton)this.findViewById(R.id.confirm2);
		cancelButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.endturn));
		
		ImageButton skill1 = (ImageButton)this.findViewById(R.id.talent1);
		ImageButton skill2 = (ImageButton)this.findViewById(R.id.talent2);
		
		GameView gameview = (GameView)findViewById(R.id.gameView);
		gameview.setPlayerOrder((Vector)playerOrder.clone());	
		gameview.setMe(me);

		
		if(passiveSkills.contains(me.getSkill1()))
		{
			skill1.setClickable(false);
		}
		if(passiveSkills.contains(me.getSkill2()))
		{
			skill2.setClickable(false);
		}
		
		gameview.setPlayerImageHashtables(playerActive1, playerInactive1, playerActive2, playerInactive2);
		gameview.setButton(okButton, cancelButton, skill1, skill2, animation, endImage);
		gameview.setMyCards(myCard1, myCard2, myCard3, myCard4, myCard5, myCard6, myCard7, myCard8);
		gameview.setActivity(this);
		gameview.init();

		
		GameThread gameThread = gameview.getThread();
		gameThread.setState(gameThread.STATE_RUNNING);
	}

	private int getPosition(int p)
	{
		int position = myposition + p;
    	if(position >= 6) position -= 6;
    	
    	return position;
	}
	
	private void updatePlayerImage(Player p)
	{
		if (p.getSelected())
		{
			if(p.getTeam() == 1)
				p.getPortrait().setBackgroundDrawable(getResources().getDrawable((Integer)playerActive1.get(p.getpClass())));
			else
				p.getPortrait().setBackgroundDrawable(getResources().getDrawable((Integer)playerActive2.get(p.getpClass())));
		}
			
		else 
		{
			if(p.getTeam() == 1)
				p.getPortrait().setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive1.get(p.getpClass())));
			else
				p.getPortrait().setBackgroundDrawable(getResources().getDrawable((Integer)playerInactive2.get(p.getpClass())));
		}
	}
	
	
	private void initPlayers()
	{
		initPlayerDeck();
		for (int i = 0; i < playerNumber; i++)
		{
			Player player = new Player();
			
			player.setpClass((String)classDeck.elementAt(i));

			player.setCurrentHP(Constants.InitialHP);
			player.setName("Player " + String.valueOf(i));
			
			if ( i == 0 || i == 3 || i == 4)
			{
				player.setTeam(1);
			}
			else 
			{
				player.setTeam(2);
			}
			playerOrder.add(player);
		} 
	}
	
	private void initPlayerDeck()
	{
		classDeck = new Vector();
		classDeck.add(Constants.classAlchemist);
		classDeck.add(Constants.classAssassin);
		classDeck.add(Constants.classAvenger);
		classDeck.add(Constants.classBard);
		classDeck.add(Constants.classFighter);
		classDeck.add(Constants.classIllusionist);
		classDeck.add(Constants.classPaladin);
		classDeck.add(Constants.classPriestess);
		classDeck.add(Constants.classProphet);
		classDeck.add(Constants.classRanger);
		classDeck.add(Constants.classSage);
		classDeck.add(Constants.classSorceress);
		classDeck.add(Constants.classVampire);
		
		Collections.shuffle(classDeck);
	}
	
	private void initSkills()
	{
		passiveSkills.add(Constants.skill1);
		passiveSkills.add(Constants.skill2);
		passiveSkills.add(Constants.skill3);
		passiveSkills.add(Constants.skill4);
		passiveSkills.add(Constants.skill5);
		passiveSkills.add(Constants.skill6);
		passiveSkills.add(Constants.skill7);
		passiveSkills.add(Constants.skill10);
		passiveSkills.add(Constants.skill12);
		passiveSkills.add(Constants.skill19);
		passiveSkills.add(Constants.skill20);
		passiveSkills.add(Constants.skill24);
		passiveSkills.add(Constants.skill25);
		passiveSkills.add(Constants.skill27);
		passiveSkills.add(Constants.skill28);
		
		
		activeSkills.add(Constants.skill8);
		activeSkills.add(Constants.skill9);
		activeSkills.add(Constants.skill11);
		activeSkills.add(Constants.skill13);
		activeSkills.add(Constants.skill14);
		activeSkills.add(Constants.skill17);
		activeSkills.add(Constants.skill18);
		activeSkills.add(Constants.skill21);
		activeSkills.add(Constants.skill22);
		activeSkills.add(Constants.skill23);
		activeSkills.add(Constants.skill26);
	}
	
	private void initHashtable()
	{
		playerInactive1 = new Hashtable();
		playerInactive1.put(Constants.classAlchemist, R.drawable.alchemist_inactive_1);
		playerInactive1.put(Constants.classAssassin, R.drawable.assassin_inactive_1);
		playerInactive1.put(Constants.classAvenger, R.drawable.avenger_inactive_1);
		playerInactive1.put(Constants.classBard, R.drawable.bard_inactive_1);
		playerInactive1.put(Constants.classFighter, R.drawable.warrior_inactive_1);
		playerInactive1.put(Constants.classIllusionist, R.drawable.illusionist_inactive_1);
		playerInactive1.put(Constants.classPaladin, R.drawable.paladin_inactive_1);
		playerInactive1.put(Constants.classPriestess, R.drawable.priestess_inactive_1);
		playerInactive1.put(Constants.classProphet, R.drawable.prophet_inactive_1);
		playerInactive1.put(Constants.classRanger, R.drawable.ranger_inactive_1);
		playerInactive1.put(Constants.classSage, R.drawable.sage_inactive_1);
		playerInactive1.put(Constants.classSorceress, R.drawable.sorceress_inactive_1);
		playerInactive1.put(Constants.classVampire, R.drawable.vampire_inactive_1);
		
		playerInactive2 = new Hashtable();
		playerInactive2.put(Constants.classAlchemist, R.drawable.alchemist_inactive_2);
		playerInactive2.put(Constants.classAssassin, R.drawable.assassin_inactive_2);
		playerInactive2.put(Constants.classAvenger, R.drawable.avenger_inactive_2);
		playerInactive2.put(Constants.classBard, R.drawable.bard_inactive_2);
		playerInactive2.put(Constants.classFighter, R.drawable.warrior_inactive_2);
		playerInactive2.put(Constants.classIllusionist, R.drawable.illusionist_inactive_2);
		playerInactive2.put(Constants.classPaladin, R.drawable.paladin_inactive_2);
		playerInactive2.put(Constants.classPriestess, R.drawable.priestess_inactive_2);
		playerInactive2.put(Constants.classProphet, R.drawable.prophet_inactive_2);
		playerInactive2.put(Constants.classRanger, R.drawable.ranger_inactive_2);
		playerInactive2.put(Constants.classSage, R.drawable.sage_inactive_2);
		playerInactive2.put(Constants.classSorceress, R.drawable.sorceress_inactive_2);
		playerInactive2.put(Constants.classVampire, R.drawable.vampire_inactive_2);
		
		playerActive1 = new Hashtable();
		playerActive1.put(Constants.classAlchemist, R.drawable.alchemist_active_1);
		playerActive1.put(Constants.classAssassin, R.drawable.assassin_active_1);
		playerActive1.put(Constants.classAvenger, R.drawable.avenger_active_1);
		playerActive1.put(Constants.classBard, R.drawable.bard_active_1);
		playerActive1.put(Constants.classFighter, R.drawable.warrior_active_1);
		playerActive1.put(Constants.classIllusionist, R.drawable.illusionist_active_1);
		playerActive1.put(Constants.classPaladin, R.drawable.paladin_active_1);
		playerActive1.put(Constants.classPriestess, R.drawable.priestess_active_1);
		playerActive1.put(Constants.classProphet, R.drawable.prophet_active_1);
		playerActive1.put(Constants.classRanger, R.drawable.ranger_active_1);
		playerActive1.put(Constants.classSage, R.drawable.sage_active_1);
		playerActive1.put(Constants.classSorceress, R.drawable.sorceress_active_1);
		playerActive1.put(Constants.classVampire, R.drawable.vampire_active_1);
		
		playerActive2 = new Hashtable();
		playerActive2.put(Constants.classAlchemist, R.drawable.alchemist_active_2);
		playerActive2.put(Constants.classAssassin, R.drawable.assassin_active_2);
		playerActive2.put(Constants.classAvenger, R.drawable.avenger_active_2);
		playerActive2.put(Constants.classBard, R.drawable.bard_active_2);
		playerActive2.put(Constants.classFighter, R.drawable.warrior_active_2);
		playerActive2.put(Constants.classIllusionist, R.drawable.illusionist_active_2);
		playerActive2.put(Constants.classPaladin, R.drawable.paladin_active_2);
		playerActive2.put(Constants.classPriestess, R.drawable.priestess_active_2);
		playerActive2.put(Constants.classProphet, R.drawable.prophet_active_2);
		playerActive2.put(Constants.classRanger, R.drawable.ranger_active_2);
		playerActive2.put(Constants.classSage, R.drawable.sage_active_2);
		playerActive2.put(Constants.classSorceress, R.drawable.sorceress_active_2);
		playerActive2.put(Constants.classVampire, R.drawable.vampire_active_2);
	}
	
}
